package com.kingdee.pda.ui.sn_query_edit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.basecommon.http.MyObserver;
import com.example.basecommon.http.RxHelper;
import com.example.basecommon.http.rxjava.BaseResponse;
import com.example.basecommon.http.rxjava.HttpUtils;
import com.example.basecommon.utils.ConstantUtil;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.Api;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.ui.popupwindow.ListSelectorPpWindow;
import com.kingdee.pda.util.CommonUtil;


import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 序列号修改
 * Created by dlj
 * DATE 2021/12/10
 * TIME 11:25
 */
public class SnEditActivity extends BaseTitleActivity {
    @BindView(R.id.et_status)
    EditText et_status;

    @BindView(R.id.tv_itemSkuNumber)
    TextView tv_itemSkuNumber;
    @BindView(R.id.tv_snNo)
    TextView tv_snNo;
    @BindView(R.id.tv_serialNo)
    TextView tv_serialNo;
    @BindView(R.id.tv_bSNNo)
    TextView tv_bSNNo;
    @BindView(R.id.tv_bSerialNo)
    TextView tv_bSerialNo;
    @BindView(R.id.tv_ownerName)
    TextView tv_ownerName;
    private ItemSNInfoVo snInfoVo;
    private List<SnStatus> statusList;
    private SnStatus snStatusBean;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, SnEditActivity.class));
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_sn_edit);

        statusList = CommonUtil.getSnStatusList();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("SN状态编辑");
        snInfoVo = (ItemSNInfoVo) getIntent().getSerializableExtra("data");
        if (snInfoVo != null) {
            tv_itemSkuNumber.setText(snInfoVo.getItemSkuNumber());
            et_status.setText(CommonUtil.getStatusName(snInfoVo.getSnStatus()));
            tv_snNo.setText(snInfoVo.getSnNo());
            tv_serialNo.setText(snInfoVo.getSerialNo());
            tv_bSNNo.setText(snInfoVo.getbSNNo());
            tv_bSerialNo.setText(snInfoVo.getbSerialNo());
            tv_ownerName.setText(snInfoVo.getOwnerName());
        }
    }

    @OnClick({R.id.btn_edit, R.id.iv_status_select})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_edit:
                if (snStatusBean == null) {

                } else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("snno", snInfoVo.getSnNo());
                    map.put("snStatus", snStatusBean.getStatus());

                    editStatusRequest(map);
                }
                break;

            case R.id.iv_status_select:
                showSnStatusSelector();
                break;
            default:
                break;
        }
    }

    private void editStatusRequest(HashMap<String, Object> map) {
        MyObserver myObserver = new MyObserver(this) {
            @Override
            public void onSuccess(BaseResponse result) {
                SpeakUtils.speakWithToast(SnEditActivity.this,"状态修改成功", false, true);
                finish();
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                SpeakUtils.speakWithToast(SnEditActivity.this, msg, false, false);
            }
        };
        HttpUtils.request(Api.class)
                .baseNoResultMethod(ConstantUtil.PDA_SN_SAVE, map)
                .compose(RxHelper.observableIO2Main(this))
                .subscribe(myObserver);
    }


    /**
     * 展示状态选择框
     */
    private void showSnStatusSelector() {
        ListSelectorPpWindow<SnStatus> locationSelector = new ListSelectorPpWindow<SnStatus>(this,
                R.drawable.shape_bg_edit_selected,
                et_status.getWidth(),
                statusList, new ListSelectorPpWindow.onItemSelectedListener() {
            @Override
            public void onItemSelected(Object item) {
                snStatusBean = (SnStatus) item;
                et_status.setText(snStatusBean.getDes());
            }
        });
        locationSelector.showAsDropDown(et_status, 0, 10);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {
    }
}
